﻿#region Copyright (c) 2010 R. Uittenbosch
/*
* Copyright (C) 2010 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace TouchUI.System.Mobile.Mvc
{
  public class AlphabetBar : Control
  {
    public AlphabetBar()
    {
      BackColor = Color.Transparent;
    }


    /// <summary>
    /// The actual boundaries of the image.
    /// </summary>
    public Rectangle Bounds { get; private set; }

    /// <summary>
    /// The actual image of the bar.
    /// </summary>
    public Bitmap Image { set; private get; }

    /// <summary>
    /// Is the bar visible?
    /// </summary>
    public bool? BarIsVisible { get; set; }

    /// <summary>
    /// Should we even try to display?
    /// </summary>
    private void DetermineVisibility()
    {
      if (Image == null)
        BarIsVisible = false;
      if (BarIsVisible.HasValue)
        return;
     
        BarIsVisible = true;
    }

    /// <summary>
    /// Determine the bounds of the image.
    /// </summary>
    private void DetermineBounds()
    {

      int top = ((Rectangle.Height - Image.Height) / 2);
      if (top < 0) top = 0;
      int left = (Rectangle.Width - (Image.Width * 2));
      int height = Rectangle.Height > Image.Height?Image.Height : Rectangle.Height;

      Bounds = new Rectangle(left, top, Image.Width * 2, height);
    }

   

    /// <summary>
    /// Paint event occured
    /// </summary>
    /// <param name="g"></param>
    public override void Paint(Graphics g)
    {
      DetermineVisibility();
      DetermineBounds();

      if (!BarIsVisible.Value)
        return;
      // Draw the image with a white transparent color.
      ImageAttributes a = new ImageAttributes();
      a.SetColorKey(Color.White, Color.White);
      g.DrawImage(Image, Bounds, 0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel, a);

    }

    public override void MouseUp(Point p)
    {
      if (Bounds.Inside(p))
      {
        int charA = (int)'A';
       
        // ypos inside image.
        int ypos = p.Y - Bounds.Y;
        int selected = charA + (ypos / (Bounds.Height / 26));
        if (Selected != null)
          Selected(this, new SelectedEventArgs() { Char = Convert.ToChar(selected) } );
      }
    }

    public event EventHandler<SelectedEventArgs> Selected;

    /// <summary>
    /// Resize event occured
    /// </summary>
    /// <param name="r"></param>
    public override void Resize(Rectangle r)
    {
      this.Rectangle = r;
    }
    
  }

  public class SelectedEventArgs : EventArgs
  {
    public char Char { get; set; }
  }
}
